package com.weifangfirecontrolformal.weifangfirecontrolformal.common.base;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.*;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.utils.appkit.internetofthings.*;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.model.RespEntity;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class BaseService {
    @Resource
    HttpServletRequest httpServletRequest;
    HttpResponse httpResponse;
    private static final Logger logger = LoggerFactory.getLogger(BaseService.class);


    public List<?> toTimeStr(List<? extends Map> list, String datePattern) {
        for (Map map : list) {
            map.forEach((k, v) -> {
                if (v instanceof Timestamp) {
                    map.put(k, DateUtil.format(new Date(((Timestamp) v).getTime()), datePattern));
                }
            });
        }
        return list;
    }

    public Map<String, Object> toTimeStr(Map param, String datePattern) {
        param.forEach((k, v) -> {
            if (v instanceof Timestamp) {
                param.put(k, DateUtil.format(new Date(((Timestamp) v).getTime()), datePattern));
            }
        });
        return param;
    }

    public Map<String, Object> toTimeStr(Map param) {
        return toTimeStr(param, DatePattern.NORM_DATE_PATTERN);
    }

    public List<?> toTimeStr(List<? extends Map> list) {
        return toTimeStr(list, DatePattern.NORM_DATE_PATTERN);
    }

//    public IPage toTimeStr(IPage iPage, String datePattern) {
//        List list = iPage.getRecords();
//        iPage.setRecords(toTimeStr(list, datePattern));
//        return iPage;
//    }
//
//    public IPage toTimeStr(IPage iPage) {
//        return toTimeStr(iPage, DatePattern.NORM_DATE_PATTERN);
//    }

    //事务相关开始

    protected int batchCount = 1000;// 每批commit的个数

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 获取session
     *
     * @author <br>
     * @return session对象
     */
    public SqlSession getSqlSession() {
        return sqlSessionFactory.openSession(ExecutorType.BATCH, false);
    }

    /**
     * 获取request操作对象
     *
     * @return request操作对象
     */
    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //return httpServletRequest;
    }

    /**
     * 获取request操作对象
     *
     * @return request操作对象
     */
    protected HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }

    /**
     * 获取session操作对象
     *
     * @return session操作对象
     */
    protected HttpSession getSession() {
        return getRequest().getSession();
    }

    public <A> A getMapper(SqlSession session, Class<A> type) {
        return session.getMapper(type);
    }

    public void commit(SqlSession session) {
        if (session != null) {
            session.commit();
            // 清理缓存，防止溢出
            session.clearCache();
        }
    }

    public void rollback(SqlSession session) {
        if (session != null)
            session.rollback();
    }

    public void closeSession(SqlSession session) {
        if (session != null)
            session.close();
    }
    /**
     * 大量数据批量插入
     *
     * @author <br>
     *
     */
    public class BigData {
        List<?> list = new ArrayList<>();

        public BigData(List<?> list) {
            this.list = list;
        }

        public int getNum() {
            return (int) Math.ceil(list.size() / (batchCount*1.0));
        }

        @SuppressWarnings("unchecked")
        public <TNT> TNT getData(int i) {
            return (TNT) list.subList(i * batchCount, ((i + 1) * batchCount) > list.size() ? list.size():(i + 1) * batchCount);
        }
    }

    //获取用户id
    public String getUserId(){
        return "1";
    }
    //获取部门id
    public String getDeptId(){
        return "1";
    }
    //获取机构id
    public String getOrgId(){
        return "1";
    }

    // rest调用包装开始----------------------------------------------------------------------------------------------------------------------------

    //rest请求:java版
    public RespEntity restJava(JSONObject jsonParam,String configKey,String key,String gp){
        System.out.println();
        String ip = ConfigKit.getStr(configKey,"config");
        String methodPath = getMethodPath(configKey,key,gp);
        String domain = ip + methodPath;
        String xml = "";
        logger.info("----------------regionList-----------------"+domain+"-----------------");
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .body(jsonParam.toJSONString(1))
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else if (jsonObject.containsKey("data")){
            return RespEntity.success(jsonObject.get("data"));
        }else {
            return RespEntity.success();
        }
    }
    //rest请求:维保模块 返回
    public com.alibaba.fastjson.JSONObject restJavaReturn(JSONObject jsonParam,String configKey,String key,String gp){
        System.out.println();
        String ip = ConfigKit.getStr(configKey,"config");
        String methodPath = getMethodPath(configKey,key,gp);
        String domain = ip + methodPath;
        String xml = "";
        logger.info("----------------regionList-----------------"+domain+"-----------------");
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .body(jsonParam.toJSONString(1))
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        return jsonObject;
    }
    //检查工作下发:java版
    public RespEntity inspectionworkJava(JSONObject jsonParam,String key,String gp){
        System.out.println();
        String ip = ConfigKit.getStr("inspectionworkJava","config");
        String methodPath = InspectionworkKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .body(jsonParam.toJSONString(1))
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //通知公告
    public RespEntity noticeannouncementJava(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("noticeannouncementJava","config");
        String methodPath = NoticeannouncementKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .body(jsonParam.toJSONString(1))
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }
    //通知模块
    public RespEntity communicationJava(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("communicationJava","config");
        String methodPath = NoticeKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .body(jsonParam.toJSONString(1))
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET") || httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }
    //系统设置
    public RespEntity systemsetup(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("systemsetup","config");
        String methodPath = SystemsetupKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            if(jsonObject.get("data")!=null){
                return RespEntity.success(jsonObject.get("data"));
            }
            return RespEntity.success();
        }
    }

    //系统设置:java
    public RespEntity systemsetupJava(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("systemsetupJava","config");
        String methodPath = SystemsetupKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .body(jsonParam.toJSONString(1))
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            if (jsonObject.containsKey("data")){
                return RespEntity.success(jsonObject.get("data"));
            }
            return RespEntity.success();
        }
    }

    //可燃气体检测
    public RespEntity combustiblegas(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("combustiblegas","config");
        String methodPath = CombustiblegasKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //可燃气体检测:java
    public RespEntity combustiblegasJava(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("combustiblegasJava","config");
        String methodPath = CombustiblegasKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //电气火灾检测
    public RespEntity electricfire(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("electricfire","config");
        String methodPath = ElectricfireKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //电气火灾检测：java
    public RespEntity electricfireJava(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("electricfireJava","config");
        String methodPath = ElectricfireKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //消防物联网
    public RespEntity fireFightingiot(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("fireFightingiot","config");
        String methodPath = FireFightingIOTKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            if(jsonObject.get("data")!=null){
                return RespEntity.success(jsonObject.get("data"));
            }
            return RespEntity.success();
        }
    }

    //消防物联网：java
    public RespEntity fireFightingiotJava(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("fireFightingiotJava","config");
        String methodPath = FireFightingIOTJavaKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .body(jsonParam.toJSONString(1))
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            if(jsonObject.get("data")!=null){
                return RespEntity.success(jsonObject.get("data"));
            }
            return RespEntity.success();
        }
    }
    //消防可视化检测
    public RespEntity firefightingvisualization(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("firefightingvisualization","config");
        String methodPath = FirefightingvisualizationKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //消防可视化检测：java
    public RespEntity firefightingvisualizationJava(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("firefightingvisualizationJava","config");
        String methodPath = FirefightingvisualizationKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //建筑消防用水
    public RespEntity firewater(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("firewater","config");
        String methodPath = FirewaterKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //建筑消防用水:java
    public RespEntity firewaterJava(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("firewaterJava","config");
        String methodPath = FirewaterKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //智慧独立烟感模块
    public RespEntity independentsmoke(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("independentsmoke","config");
        String methodPath = IndependentsmokeKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //智慧独立烟感模块:java
    public RespEntity independentsmokeJava(JSONObject jsonParam,String key,String gp){
        String ip = ConfigKit.getStr("independentsmokeJava","config");
        String methodPath = IndependentsmokeKit.getStr(key,gp);
        String domain = ip + methodPath;
        String xml = "";
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success();
        }
    }

    //组装数据
    public com.alibaba.fastjson.JSONObject makeData(JSONObject jsonParam,String configKey,String configPg,String key,String gp){
        String ip = ConfigKit.getStr(configKey,configPg);
        String methodPath = getMethodPath(configKey,key,gp);
        String domain = ip + methodPath;
        String xml = "";
        httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401){
            com.alibaba.fastjson.JSONObject errorObject = new com.alibaba.fastjson.JSONObject();
            errorObject.put("methodCodeFlag","1");
            return errorObject;
        }
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            com.alibaba.fastjson.JSONObject errorObject = new com.alibaba.fastjson.JSONObject();
            errorObject.put("msg",jsonObject.getString("msg"));
            errorObject.put("methodErrorFlag","1");
            return errorObject;
        }
        return jsonObject.getJSONObject("data");
    }
    //组装数据:处理返回即可能是JSONObject又可能是JsonArray的情况
    public JSON makeDataForJSON(JSONObject jsonParam,String configKey,String configPg,String key,String gp){
        String ip = ConfigKit.getStr(configKey,configPg);
        String methodPath = getMethodPath(configKey,key,gp);
        String domain = ip + methodPath;
        String xml = "";
        httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401){
            com.alibaba.fastjson.JSONObject errorObject = new com.alibaba.fastjson.JSONObject();
            errorObject.put("methodCodeFlag","1");
            return errorObject;
        }
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            com.alibaba.fastjson.JSONObject errorObject = new com.alibaba.fastjson.JSONObject();
            errorObject.put("msg",jsonObject.getString("msg"));
            errorObject.put("methodErrorFlag","1");
            return errorObject;
        }
        return jsonObject.getJSONArray("data");
    }
    //rest请求:java版
    public RespEntity weChatCode(JSONObject jsonParam,String configKey,String key,String gp){
        System.out.println();
        String ip = ConfigKit.getStr(configKey,"config");
        String methodPath = getMethodPath(configKey,key,gp);
        String domain = ip + methodPath;
        String xml = "";
        logger.info("----------------regionList-----------------"+domain+"-----------------");
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            httpResponse = HttpRequest.get(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .form(jsonParam)
                    .execute();
        }else if(httpServletRequest.getMethod().equalsIgnoreCase("POST")){
            httpResponse = HttpRequest.post(domain)
                    .header("Authorization", httpServletRequest.getHeader("Authorization"))
                    .body(jsonParam.toJSONString(1))
                    .execute();
        }
        xml = httpResponse.body();
        int state = httpResponse.getStatus();
        if(state == 401 || JSON.parseObject(xml).getInteger("code")==401) return RespEntity.expire();
        com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(xml);
        if(jsonObject.getInteger("code")==1){
            return RespEntity.fail(jsonObject.getString("msg"));
        }
        if(httpServletRequest.getMethod().equalsIgnoreCase("GET")){
            return RespEntity.success(jsonObject.get("data"));
        }else{
            return RespEntity.success(jsonObject.getString("data"));
        }
    }
    //获取方法路径
    public String getMethodPath(String configKey,String key,String gp){
        String methodPath = "";
        if(configKey.equals("business") || configKey.equals("businessJava")) methodPath = BusinessKit.getStr(key,gp);
        if(configKey.equals("check") || configKey.equals("checkJava")) methodPath = CheckKit.getStr(key,gp);
        if(configKey.equals("inspectionwork") || configKey.equals("inspectionworkJava")) methodPath = InspectionworkKit.getStr(key,gp);
        if(configKey.equals("noticeannouncement") || configKey.equals("noticeannouncementJava")) methodPath = NoticeannouncementKit.getStr(key,gp);
        if(configKey.equals("systemsetup") || configKey.equals("systemsetupJava")) methodPath = SystemsetupKit.getStr(key,gp);
        if(configKey.equals("combustiblegas") || configKey.equals("combustiblegasJava")) methodPath = CombustiblegasKit.getStr(key,gp);
        if(configKey.equals("electricfire") || configKey.equals("electricfireJava")) methodPath = ElectricfireKit.getStr(key,gp);
        if(configKey.equals("fireFightingiot") || configKey.equals("fireFightingiotJava")) methodPath = FireFightingIOTKit.getStr(key,gp);
        if(configKey.equals("firefightingvisualization") || configKey.equals("firefightingvisualizationJava")) methodPath = FirefightingvisualizationKit.getStr(key,gp);
        if(configKey.equals("firewater") || configKey.equals("firewaterJava")) methodPath = FirewaterKit.getStr(key,gp);
        if(configKey.equals("independentsmoke") || configKey.equals("independentsmokeJava")) methodPath = IndependentsmokeKit.getStr(key,gp);
        if(configKey.equals("basisInfo")|| configKey.equals("systemSetting") || configKey.equals("task") || configKey.equals("dataAnalysis")) methodPath = MaintenanceSystemKit.getStr(key,gp);
        return methodPath;
    }

// rest调用包装结束----------------------------------------------------------------------------------------------------------------------------

}
